#include "CLink/Router/NotFoundHandler.h"
#include "CLink/Utils/ResponseUtils.h"

namespace CLink
{
namespace Router
{

NotFoundHandler::NotFoundHandler(const std::string &customMessage)
    : m_customMessage(customMessage)
{
}

bool NotFoundHandler::handle(RequestContext &context)
{
    // 准备错误消息
    std::string message = m_customMessage.empty() ? "The requested resource was not found" : m_customMessage;

    // 创建错误响应
    CLink::HttpResponseUtils::sendErrorJson(context.response,
                                            404,
                                            "Not Found",
                                            message);

    return true;
}

bool NotFoundHandler::matches(const RequestContext &context) const
{
    // 404处理器总是匹配，因为它是一个兜底处理器
    return true;
}

int NotFoundHandler::getPriority() const
{
    // 返回最低优先级，确保它只在没有其他处理器匹配时才被使用
    return std::numeric_limits<int>::min();
}

NotFoundHandler &NotFoundHandler::setCustomMessage(const std::string &message)
{
    m_customMessage = message;
    return *this;
}

} // namespace Router
} // namespace CLink